<?php 
	//timer pengecekan load page...
	include("inc/timer.php");
	$_start_timer = get_microtime();
	 
	//check protect1 jika session da tidak valid, balikin ke index.php
	//jika valid y biarin saja
	
	require_once("protect1.php");
	require_once("inc/connect.php");
	require_once("inc/settings.php");
	require_once("inc/function.php");
	require_once("inc/function-html.php");

	//pindahkan ke lokal variable
	$_kodemk = $_GET["kodemk"];
	$_nrp = $reg_uname;
	$_semester = $_global_settings['semester']->value;
	$_tahun_ajaran = $_global_settings['tahun_ajaran']->value;						
	
	// $pilih=mysql_select_db("uwika_krs",$conn);
	//$pdo->query("USE uwika_krs");

	$sql = "SELECT nama FROM biodata WHERE nrp = :reg_uname ";
	$stmt_mahasiswa = $pdo->prepare($sql);
	$stmt_mahasiswa->execute(array(':reg_uname' => $_nrp )) or die("ERROR: " . implode(":", $stmt_mahasiswa->errorInfo())); //reg jurusan dari protect1.php
	$_data_mahasiswa = $stmt_mahasiswa->fetch(PDO::FETCH_OBJ);
	
	//protect2 
	//cheking semester.. biar gk aneh2 mbukak.. prevent from sql injection
	//JANGAN dihapus, karna ini skaligus Query data2 Matakuliah (digabung dengan protect2)
	$sql = "SELECT semester, dosen, pagu, keterangan, mk.nama, mk.sks, penawaran.kodemk, penawaran.hari, penawaran.mulaipukul, penawaran.selesaipukul 
			FROM penawaran, mk where penawaran.kodemk = :kodemk and sesi = :sesi 
			AND periode = :periode and penawaran.kodemk = mk.kodemk
			AND jurusan = :reg_jurusan
			";
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array(':kodemk' => $_kodemk
							,':sesi' => $_semester
							,':periode' => $_tahun_ajaran
							,':reg_jurusan' => $reg_jurusan));

	$_data_matakuliah = $stmt->fetchAll(PDO::FETCH_OBJ);

	if(empty($_data_matakuliah->kodemk)){
		//dunno for what
	}
	
	$masa_krs = false;
	if (date("Y-m-d H:i:s") >= $_global_settings['waktu_krs_mulai']->value && date("Y-m-d H:i:s") <= $_global_settings['waktu_krs_selesai']->value){ // batasan waktu (jam 30 agust 13.00.01 - 14 sept 23.59.59)
		$masa_krs = true;
	}
	
	$masa_validasi = false;
	$sql = "SELECT meta_key, meta_value FROM user_metadata
			WHERE username = :username and meta_key = :meta_key 
			";
	$stmt_validasi = $pdo->prepare($sql);
	$stmt_validasi->execute(array(':username' => $_nrp
							,':meta_key' => 'validasi'));
	$_data_validasi = $stmt_validasi->fetch(PDO::FETCH_OBJ);
	if( ($_data_validasi->meta_value == 1) || ($_data_validasi->meta_value == 2) ){ // 1. sudah di stujui mahasiswa, 2. sudah d stujui dosen
		//gagal, karna tidak ad MK tersebut untuk DIA
		$masa_validasi = false;
	}else{
		//sukses, karna tidak ad MK tersebut untuk DIA
		$masa_validasi = true;
	}

	$masa_blokir = true;
	/* cek level mahasiswa */
	$sqlstr_checking_level_blokir = "SELECT username, password, level FROM security WHERE username=:uname";
	$stmt_checking_level_blokir = $pdo->prepare($sqlstr_checking_level_blokir);
	$stmt_checking_level_blokir->execute(array(':uname' => $_nrp));
	//because of php 5.2 $_POST['name'] = $name, that makes me difficult for working -_-
	if($row_checking_level_blokir = $stmt_checking_level_blokir->fetch(PDO::FETCH_OBJ)){
		if($row_checking_level_blokir->level != 1){
			$masa_blokir = true;
		}else{
			$masa_blokir = false;
		}
	}
	/* finish */
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Home !</title>
<link rel="stylesheet" type="text/css" href="css/reset_main.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<style type="text/css">
	body{ behavior:url(cssHoverFix.htc);}
	#div a { normal state }
	#div a:hover { alternative state }
</style>
</head>
<body>	
	<header id="main">
		<table id="header">
			<tr>
				<td class="header_logo_small"><a class="header_logo_small" href="#"></a></td>
				<td class="header_announce">
					<marquee style="letter-spacing:1px;marquee-speed:slow;">
						Nilai <strong>UTS</strong> Semester Gasal Periode 2012-2013 sudah keluar *sebagian. 
						Jika anda menemukan Bug / Kritik / Saran, silahkan Laporkan kepada ICT Sesegera mungkin. 
						Terima Kasih.
					</marquee>
				</td>
				<td class="header_user_name"><a class="header_user_name_a" href="#"><?php echo (strlen($_data_mahasiswa->nama) < 15 ? ucwords(strtolower($_data_mahasiswa->nama)) : ucwords(strtolower(substr($_data_mahasiswa->nama,0,15)))."..."); ?></a></td>
				<td class="header_user_photo">
					<a class="header_user_photo_a" href="#">
						<img class="header_user_photo_img" src="images/people/default.gif">
					</a>
				</td>
			</tr>
		</table>
	</header>
	<table class="board">
		<tr>
			<td class="sidebar">
				<div id="navigation">
					<ul class="top-level">
						<?php
							$_level_settings = getLevelSettings($reg_level, $pdo);
							$basename_url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
							if(count($_level_settings->menu) > 0){
								foreach($_level_settings->menu as $foreach){
									if ($basename_url == $foreach->url){$clicked = 'class="clicked"';}else{$clicked = '';}
									echo '<li><a href="'.$foreach->url.'" '.$clicked.'>'.$foreach->nama.'</a></li>';
								}
							}else{ echo '<li><a href="#" class="clicked">Home</a></li>'; }
						?>

					</ul>
				</div>
			</td>
			<td class="bigbox">
				<div class="banner"></div>
				<div class="content letter-spacing">	
					<?php if($masa_blokir){ ?>
						<div class="warning red">
							<label>Account Diblokir !</label>
							Mohon Maaf, Account Anda untuk sementara waktu ini kami <strong>BLOKIR</strong>
							Jika anda belum menyelesaikan biaya daftar ulang / biaya denda atas keterlambatan KRS, silahkan menghubungi Biro Administrasi Keuangan.
							Terimakasih
						</div>
					<?php } ?>
					<br \>
					<h1>Informasi Mata Kuliah</h1><br \>
					<table class="data head ">
						<tr>
							<td width="15%">Kode</td><td width="2%">:</td><td width="35%"><?php echo $_data_matakuliah[0]->kodemk; ?></td>
							<td width="15%">Semester / SKS</td><td width="2%">:</td><td width="35%"><?php echo $_data_matakuliah[0]->semester." / <code>".$_data_matakuliah[0]->sks ?> SKS</code></td>
						</tr>
						<tr>
							<td>Mata Kuliah</td><td>:</td><td><?php echo $_data_matakuliah[0]->nama; ?></td>
							<td>Periode</td><td>:</td><td><?php echo $_semester; ?> / <?php echo $_tahun_ajaran; ?></td>
						</tr>
						<tr>
							<td>Dosen</td><td>:</td><td><?php echo $_data_matakuliah[0]->dosen; ?></td>
							<td>Kuota Kelas</td><td>:</td><td><?php echo $function->pagu2kuotakelas($_data_matakuliah[0]->pagu); ?></td>
						</tr>
						<tr>
							<td>Hari / Pukul</td><td>:</td><td><?php echo $_data_matakuliah[0]->hari." / ".$_data_matakuliah[0]->mulaipukul." - ".$_data_matakuliah[0]->selesaipukul;?> WIB</td>
							<td>Keterangan</td><td>:</td><td><?php echo $_data_matakuliah[0]->keterangan; ?></td>
						</tr>
						<tr style="vertical-align:middle;">
							<td>Aksi Anda</td><td>:</td>	
							<td colspan="4">
								<table><tr>
									<?php
										if ( ($masa_krs) && ($masa_validasi) && (!$masa_blokir) ){
											/* Masih masa KRS-an */
											$sql = "SELECT nrp FROM registrasi WHERE nrp = :reg_uname AND kodemk = :kodemk AND periode = :select_periode AND sesi = :select_sesi";
											$stmt_terdaftar = $pdo->prepare($sql);
											$stmt_terdaftar->execute(array(':reg_uname' => $_nrp
																	,':kodemk' => $_kodemk
																	,':select_periode' => $_tahun_ajaran
																	,':select_sesi' => $_semester));
											$_data_terdaftar = $stmt_terdaftar->fetch(PDO::FETCH_OBJ);
											if (!empty($_data_terdaftar->nrp)){ 
												//gagal sudah terdaftar
												?>
										<td class="pointer" style="padding-right:5px;">
											<form name="frmdaftar" method="POST" action="krs_batal.php">
												<input type="hidden" name="kodemk" value="<?php echo $_data_matakuliah[0]->kodemk; ?>">
												<input name="Submit" type="submit" value="Batalkan">
											</form>
										</td>											
												<?php
											}else{
												//belum terdaftar
												?>
										<td class="pointer" style="padding-right:5px;">
											<form name="frmdaftar" method="POST" action="krs_daftar.php">
												<input type="hidden" name="kodemk" value="<?php echo $_data_matakuliah[0]->kodemk; ?>">
												<input type="hidden" name="sks" value="<?php echo $_data_matakuliah[0]->sks; ?>">
												<input type="hidden" name="daftar_hari" value="<?php echo $_data_matakuliah[0]->hari; ?>">
												<input type="hidden" name="daftar_mulaipukul" value="<?php echo $_data_matakuliah[0]->mulaipukul; ?>">
												<input type="hidden" name="daftar_selesaipukul" value="<?php echo $_data_matakuliah[0]->selesaipukul; ?>">
												<input name="Submit" type="submit" value="Daftarkan">
											</form>
										</td>
												<?php
											}
										}else{
											/* Masa KRS-an sudah BERAKHIR, no info*/
											
										}

									?>
									<td class="pointer" >
										<form name="frmbatal" method="post" action="view_penawaran.php">
											<input type="button" value="Kembali" onClick="javascript: history.go(-1)">
										</form>
									</td>
								</tr></table>
							</td>
							<td></td>
						</tr>
					</table><br \>
					<!-- garis abu2 -->
					<table class="data body"><tfoot><tr><td></td></tr></tfoot></table>
					
					<table class="data body">
						<thead>
							<th width="4%">No</th>
							<th width="13%">NRP</th>
							<th colspan="3" width="50%">Nama</th>
							<th width="8%">Status</th>
							<th colspan="2">Tanggal Registrasi</th>
						</thead>
						<tbody>
						<?php
							$sql = "SELECT registrasi.nrp, status, nama, date_format(registrasi.tanggal,'%d-%m-%Y') as tanggal, jam 
									FROM registrasi, biodata where kodemk=:kodemk and sesi=:sesi and periode=:periode 
									AND registrasi.nrp = biodata.nrp ORDER BY registrasi.tanggal ASC, registrasi.jam ASC";
							$stmt = $pdo->prepare($sql);
							$stmt->execute(array(':kodemk' => $_kodemk
													,':sesi' => $_semester
													,':periode' => $_tahun_ajaran)); //reg jurusan dari protect1.php
							$jumlah_mahasiswa = 0;
							while($_data_matakuliah_mahasiswa = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object		
								//list($nrp,$status,$namamhs, $tanggal, $jam) = $row;
								if (empty($_data_matakuliah_mahasiswa->nrp)) {
									echo "";
									break;
								}
								?>
									<tr>
										<td class="center"><?php echo $jumlah_mahasiswa+1; ?></td>
										<td class="center"><?php echo $_data_matakuliah_mahasiswa->nrp; ?></td>
										<td align="left" colspan="3"><?php echo $_data_matakuliah_mahasiswa->nama; ?></td>
										<td class="center"><?php echo $_data_matakuliah_mahasiswa->status; ?></td>
										<td class="center"><?php echo $_data_matakuliah_mahasiswa->tanggal; ?></td>
										<td class="center"><?php echo $_data_matakuliah_mahasiswa->jam; ?></td>
									</tr>
								<?php
								$jumlah_mahasiswa++;
							}
							?>
						</tbody>
						<tfoot>
							<tr>
								<td colspan="2" >Total Mahasiswa &nbsp;</td>
								<td style="text-align:left;"><span class="sign black big border"><?php echo $jumlah_mahasiswa; ?></span></td>
								<td colspan="8"></td>
							</tr>
						</tfoot>
					</table>
					<div style="padding-bottom:50px;"></div>
				</div>
			</td>
		</tr>
	</table>
	<footer id="main">
	  <div><a href="http://www.widyakartika.ac.id/perpustakaan">Perpustakaan Online</a> | <a href="http://krs.widyakartika.ac.id/">KRS Online</a></div>
	  <div>Copyright &copy; 2012. <a href="http://www.widyakartika.ac.id">Universitas Widya Kartika</a> & <a href="#">Labkom ICT</a> - All rights reserved</div>
		<?php
			$_finish_timer = get_microtime();
			echo 'Page generated in '.round(($_finish_timer - $_start_timer), 4).' seconds.';
		?>
	</footer>
</body>
